package cn.zm.trip.web.controller;

import cn.zm.trip.web.dao.ForumDao;
import cn.zm.trip.web.dao.HotelDao;
import cn.zm.trip.web.dao.ViewPointDao;
import cn.zm.trip.web.domain.*;
import cn.zm.trip.web.service.FoodService;
import cn.zm.trip.web.service.TravelInfoService;
import cn.zm.trip.web.service.ViewPointService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.List;

import static cn.zm.trip.web.controller.ForumController.extractData;

@Controller
@RequestMapping(value = "")
public class IndexController {
	@Autowired
	private ViewPointService viewPointService;

	@Autowired
	private ViewPointDao viewPointDao;

	@Autowired
	private HotelDao hotelDao;

	@Resource
	private FoodService foodService;
	@Resource
	private TravelInfoService travelInfoService;
	@Resource
	private ForumDao forumDao;


	/**
	 * 跳转首页
	 */
	@RequestMapping(value = {"", "index"}, method = RequestMethod.GET)
	public String index(ViewPointExample example, Model model) {
		HotelExample hotelExample = new HotelExample();
//		example.setOrderByClause("tp_vid desc");
		List<ViewPoint> viewPoints = viewPointDao.listViewPointByQuery(null,0,4,"heat");

		//酒店
		hotelExample.setOrderByClause("hid desc");
		Hotel hotel = new Hotel();
		hotel.setOrderBy("heat");
		List<Hotel> hotels = hotelDao.hotelPointSearch(hotel);
		Page<Food> page = new Page<>(1, 4); // 第一页，每页显示4条记录
		Page<TravelInfo> pageTravel = new Page<>(1, 4); // 第一页，每页显示4条记录

		//美食
		LambdaQueryWrapper<Food> queryWrapper = new LambdaQueryWrapper<Food>().orderBy(true, false, Food::getHeat);
		foodService.page(page, queryWrapper);
		List<Food> foods = page.getRecords();
		//资讯
		travelInfoService.page(pageTravel, null);
		List<TravelInfo> travelInfos = pageTravel.getRecords();
		//论坛
		List<Forum> forums = forumDao.selectByExampleWithBLOBs(null);
		for (Forum forum : forums) {
			forum.setTpTImg(extractData(forum.getTpTcontent()));
		}
		model.addAttribute("forums",forums.subList(0,4));
		//传送景点
		model.addAttribute("viewPoints", viewPoints);
		model.addAttribute("hotels", hotels);
		model.addAttribute("foods", foods);
		model.addAttribute("travelInfos", travelInfos);
		return "index";
	}

}
